/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;

/**
 *
 * @author GiangLT
 */
public abstract class AbstractCrudBean extends BaseBean implements Serializable {
    
    protected boolean edit;
    protected boolean view;
    protected final Logger logger = getLogger();
    
    @PostConstruct
    public void init() {
        logger.trace("[{}] Initializing Bean: {}", getUsername(), this.getClass().getSimpleName());
        resetToCreateForm();
        logger.trace("[{}] Done initialize Bean: {}", getUsername(), this.getClass().getSimpleName());
    }
    
    protected abstract Logger getLogger();
    
    protected abstract void resetToCreateFormAction() throws Exception;
    
    protected abstract void validateForm() throws Exception;
    
    protected abstract void findSelected(int id) throws Exception;
    
    protected abstract void createSelected() throws Exception;
    
    protected abstract void editSelected() throws Exception;
    
    protected abstract void removeSelected() throws Exception;
    
    protected abstract void prepareViewAction() throws Exception;
    
    protected abstract void prepareEditAction() throws Exception;
    
    protected void logAndNotifyError(Exception ex) {
        String error = "Lỗi! ";
        logger.error("[{}]: ", getUsername(), ex);
        JsfUtil.addErrorMessage(error, ex.getMessage());
    }
    
    public void resetToCreateForm() {
        try {
            resetToCreateFormAction();
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
    }
    
    public void prepareView() {
        logger.trace("[{}] Preparing to view selected entity", getUsername());
        try {
            prepareViewAction();
//            RequestContext.getCurrentInstance().scrollTo("editForm");
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
        logger.trace("[{}] Done preparing to view selected entity", getUsername());
    }
    
    public void create() {
        logger.trace("[{}] Creating new entity", getUsername());
        try {
            validateForm();
            createSelected();
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
        logger.trace("[{}] Done creating new entity", getUsername());
    }
    
    public void prepareEdit() {
        logger.trace("[{}] Preparing to edit selected entity", getUsername());
        try {
            prepareEditAction();
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
        logger.trace("[{}] Done preparing to edit selected entity", getUsername());
    }
    
    public void edit() {
        logger.trace("[{}] Editing selected entity", getUsername());
        try {
            validateForm();
            editSelected();
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
        logger.trace("[{}] Done editing selected entity", getUsername());
    }
    
    public void remove() {
        logger.trace("[{}] Removing selected entity", getUsername());
        try {
            removeSelected();
//            RequestContext.getCurrentInstance().scrollTo("header");
        } catch (Exception ex) {
            logAndNotifyError(ex);
        }
        logger.trace("[{}] Done removing selected entity", getUsername());
    }
    
    public boolean isEdit() {
        return edit;
    }
    
    public void setEdit(boolean edit) {
        this.edit = edit;
    }
    
    public boolean isView() {
        return view;
    }
    
    public void setView(boolean view) {
        this.view = view;
    }
}
